<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>多棱柱</title>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			html,
			body {
				height: 100%;
				overflow: hidden;
			}

			#wrap {
				position: absolute;
				top: 0;
				left: 0;
				bottom: 0;
				right: 0;
				margin: auto;
				width: 300px;
				height: 300px;
				border: 2px solid;
				background-size: 100% 100%;
				perspective: 200px;
			}

			#wrap>.box {
				position: absolute;
				top: 0;
				left: 0;
				bottom: 0;
				right: 0;
				margin: auto;
				width: 100px;
				height: 100px;

				transform-style: preserve-3d;
				transition: 3s transform;
			}

			#wrap>.box>div {
				position: absolute;
				width: 100%;
				height: 100%;
				background-color: rgba(220, 78, 65, 1);
				font: 50px/100px "宋体";
				text-align: center;
				backface-visibility: hidden;
			}

			#wrap:hover .box {
				transform: rotateY(360deg);
			}
		</style>
	</head>
	<body>
		<div id="wrap">
			<div class="box">

			</div>
		</div>
		<script type="text/javascript">
			window.onload = () => {
				const num = Number(prompt("您希望创建几棱柱？"));
				createLZ(num);
			}

			function createLZ(n) {
				const boxNode = document.querySelector("#wrap>.box");
				const styleNode = document.createElement("style");
				// 外角
				const degOut = 360 / n;
				// 内角
				const degIn = 180 - degOut;
				// 棱长
				const len = boxNode.offsetWidth;

				let innerHtml = "";
				let cssText = "";
				for (let i = 0; i < n; i++) {
					innerHtml += `<div>${i+1}</div>`;
					cssText +=
						`#wrap>.box>div:nth-child(${i+1}) {
						transform: rotateY(${degOut*i}deg);
					}`;
				}

				cssText += `#wrap>.box {transform-origin: center center -${len/2*Math.tan(degIn/2*Math.PI/180)}px;}`;
				cssText += `#wrap>.box>div {transform-origin: center center -${len/2*Math.tan(degIn/2*Math.PI/180)}px;}`;
				boxNode.innerHTML = innerHtml;
				styleNode.innerHTML = cssText;
				document.head.appendChild(styleNode);
			}
		</script>
	</body>
</html>`
